class_name UnitBat
extends UnitBase

var move_target: Node2D

func pick_new_wander_target():
	# 在矩形范围内随机目标点
	var x_offset = randf_range(-wander_width/2, wander_width/2)
	var y_offset = randf_range(-wander_height/2, wander_height/2)
	target_pos = home_position + Vector2(x_offset, y_offset)
	
	# 下一次切换目标时间
	timer = randf_range(1.0, change_dir_time)
	
	
func play_wander_animation():
	animation_player.play("fly")
	

func set_move_2(target: Node2D):
	attack_target = null
	move_target = target
	states.transition("Move")


func process_move_2_target(delta: float):
	if not move_target:
		states.transition("Wander")
		return
		
	navigation_agent_2d.target_position = move_target.global_position
	
	if navigation_agent_2d.is_navigation_finished():
		return
	
	var next_pos = navigation_agent_2d.get_next_path_position()
	var dir = (next_pos - global_position).normalized()
	var target_velocity = dir * approach_speed
	if navigation_agent_2d.avoidance_enabled:
		navigation_agent_2d.set_velocity(target_velocity)
	else:
		_on_navigation_agent_2d_velocity_computed(target_velocity, delta)
